<?php

if($post_form_id=="metric") {

	$array_post = $_POST;
	$post_user_id = $_POST["active_user_id"];
	$post_datestamp = $_POST["datestamp"];
	
	$array_post_keys = array_keys($array_post);
	
	foreach($array_post_keys as $item) {
	
		if(substr($item,0,5)=="post_") {
		
			$field_name		= substr($item,5);
			$field_value	= $array_post[$item];
			
			$prompt_current = PromptPeer::retrieveByPK($field_name);
			
			$value_valid = true;
			
			if($prompt_current->getType()=="integer") {
				if(is_numeric($field_value)==false) {
					$value_valid = false;
				}
			}
			
			if($prompt_current->getType()=="date") {
				$field_value = substr($field_value,6,4) . "-" . substr($field_value,3,2) . "-" . substr($field_value,0,2);
			}

			$obj_metric = MetricPeer::retrieveByPK($post_datestamp,$field_name,$post_user_id);
			if($obj_metric==null) {
				$obj_metric = new Metric();
				$metric_added = true;
			}
			$obj_metric->setDatestamp($post_datestamp);
			$obj_metric->setPromptId($field_name);
			$obj_metric->setUserId($post_user_id);
			$obj_metric->setValue($field_value);
			$obj_metric->save();	
			
		}
	
	}
	
	if($metric_added==true) {
		array_push($ctl_status,"Metric values created for $post_datestamp.");				
	} else {
		array_push($ctl_status,"Metric values updated for $post_datestamp.");		
	}

}





$c = new Criteria();
$c->addAscendingOrderByColumn(TeamPeer::SORT_ORDER);
$array_teams = TeamPeer::doSelect($c);

$smarty_array_teams = array();

foreach($array_teams as $team) {

	$user_current = UserTeamPeer::retrieveByPK($active_user_id,$team->getId());

	if($user_current!=null) {

		array_push($smarty_array_teams,		array(
											"id"=>$team->getId(),
											"description"=>$team->getDescription(),
											"sort_order"=>$team->getSortOrder(),
											)
										);
	}
	
}

$c = new Criteria();
$c->addAscendingOrderByColumn(PromptPeer::SORT_ORDER);
$array_prompts = PromptPeer::doSelect($c);

$smarty_array_prompts = array();

foreach($array_prompts as $prompt) {
	array_push($smarty_array_prompts,	array(
										"id"=>$prompt->getId(),
										"name"=>$prompt->getName(),
										"team_id"=>$prompt->getTeamId(),
										"type"=>$prompt->getType(),
										"sort_order"=>$prompt->getSortOrder(),
										"label"=>$prompt->getLabel(),										
										"help_text"=>$prompt->getHelpText()
										)
									);
}

$smarty->assign("list_form_metric_team",$smarty_array_teams);
$smarty->assign("list_form_metric_prompt",$smarty_array_prompts);
$smarty->assign("data_form_metric_datestamp_select",make_datepicker());
						
						
?>
